<?php
/**
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*@package Box
*@subpackage Image
*/
class Box_Image_Picture
{
	private $name = "";
	private $data = false;
	private $type = false;
	private $alt = "---image---";

	private function TempImage()
	{
		if ( ($this->data!==false) && ($this->type!==false) )
		{
			$temp_name = CoreUtils::TempFolder().md5( $this->data ).".".$this->type;
			if ( !file_exists( $temp_name ) ) 
				file_put_contents( $temp_name, $this->data );
			return $temp_name;
		}
		return "none";
	}

	public function __construct( $name )
	{
		$this->name = $name;
	}

	public function ProcessData( $data )
	{
		if ( !isset($data["data"]["pictures"]) )
			$data["data"]["pictures"] = array();
		$data["data"]["pictures"][$this->name] = array("filename" => $this->TempImage(),
												"alt" => $this->alt,
												"data" => $this->data,
												"type" => $this->type,
												"name" => $this->name );
		return $data;
	}

	public function Save()
	{
		$ret = false;
		$alt = Core::PostVar($this->name."_alt",false, "string");
		if ( $alt !== false )
		{
			$this->alt = $alt;
			$ret = true;
		}
		if ( ($file = CoreUtils::UploadedFileGet( $this->name )) !== false )
		{
			$file = array_pop($file["files"]);
			$this->data = $file["data"];
			$this->type = substr( $file["type"], strpos( $file["type"], "/" )+1 );
			$ret = true;
		}
		return $ret;
	}
}
?>